package com.gc.common.util;

import java.security.MessageDigest;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 安全加解密(DES/MD5/BASE64)
 * @author zh-heng
 */
public class SecurityUtil {
	private final static String password = "&jiami!t(terw)";
	/**
	 * 加密 DES
	 * 
	 * @param strInput
	 *            输入字符串
	 * @return String
	 * @throws Exception
	 */
	public final static String encoderDES(String strInput) {
		String strResult = "";
		SecureRandom secureRandom = new SecureRandom();
		byte byteRawKeyData[] = password.getBytes();
		DESKeySpec dKeySpec = null;
		try {
			dKeySpec = new DESKeySpec(byteRawKeyData);
			SecretKeyFactory secretKeyFactory = SecretKeyFactory
					.getInstance("DES");
			SecretKey key = secretKeyFactory.generateSecret(dKeySpec);
			Cipher cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.ENCRYPT_MODE, key, secureRandom);
			byte[] cipherByte = cipher.doFinal(strInput.getBytes());
			strResult = new BASE64Encoder().encode(cipherByte);
		} catch (Exception e) {
		}
		return strResult;

	}

	/**
	 * 解密 DES
	 * 
	 * @param strInput
	 *            输入字符串
	 * @return String 如果返回是"",则解密失败
	 * @throws Exception
	 */
	public final static String decoderDES(String strInput) {
		String strResult = "";
		byte[] byteArray = null;
		try {
			byteArray = new BASE64Decoder().decodeBuffer(strInput);
			SecureRandom secureRandom = new SecureRandom();
			byte byteRawKeyData[] = password.getBytes();
			DESKeySpec dKeySpec = new DESKeySpec(byteRawKeyData);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			SecretKey key = keyFactory.generateSecret(dKeySpec);
			Cipher cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.DECRYPT_MODE, key, secureRandom);
			byte[] clearByte = cipher.doFinal(byteArray);
			strResult = new String(clearByte);
		} catch (Exception e) {
		}
		return strResult;
	}

	/**
	 * 加密 MD5
	 * 
	 * @param strInput
	 *            输入字符串
	 * @return String
	 * @throws Exception
	 */
	public final static String encoderMD5(String strInput) {
		MessageDigest messageDigest = null;
		try {
			messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.reset();
			messageDigest.update(strInput.getBytes("UTF-8"));
		} catch (Exception ex) {
		}

		byte[] byteArray = messageDigest.digest();
		StringBuffer md5StrBuff = new StringBuffer();
		for (byte b : byteArray) {
			if (Integer.toHexString(0xFF & b).length() == 1) {
				md5StrBuff.append("0").append(Integer.toHexString(0xFF & b));
			} else {
				md5StrBuff.append(Integer.toHexString(0xFF & b));
			}
		}
		return md5StrBuff.toString();
	}

	/**
	 * 用SUN自带的进行BASE 64编码
	 * 
	 * @param strInput
	 * @return String
	 */
	public static String encoderBASE64(String strInput) {
		try {
			BASE64Encoder encoder = new BASE64Encoder();
			return encoder.encode(strInput.getBytes());
		} catch (Exception ex) {
		}
		return null;
	}

	/**
	 * 将 BASE64 编码的字符串 s 进行解码
	 * 
	 * @param strInput
	 * @return String
	 */
	public static String decoderBASE64(String strInput) {

		try {
			BASE64Decoder decoder = new BASE64Decoder();
			byte[] b = decoder.decodeBuffer(strInput);
			return new String(b);
		} catch (Exception e) {
			return null;
		}
	}

		/**
	 * 先使用MD5后BASE64加密成24位
	 * @param strInput
	 * @return
	 */
	public static String encode (String strInput){
		String result = "";
		if(strInput == null){
			return result;
		}
		MessageDigest messageDigest = null;
		try {
			messageDigest = MessageDigest.getInstance("MD5");
		} catch (Exception e) {
		}
		BASE64Encoder encoder = new BASE64Encoder();
		try {
			result = encoder.encode(messageDigest.digest(strInput.getBytes("UTF-8")));
		} catch (Exception e) {
		}
		return result;
	}
}
